package de.rj.urwalking.ui.fragments.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import de.rj.urwalking.R;
import de.rj.urwalking.config.Config;
import de.rj.urwalking.ui.activities.URwalkingActivity;
import de.rj.urwalking.util.ExperimentManager;
import de.rj.urwalking.util.FileUpload;

/**
 * Displayed when the user tries to exit the app while an experiment is running.
 * 
 * @author rjj
 * 
 */
public class CancelExperimentDialog extends DialogFragment implements DialogInterface.OnClickListener {

	public CancelExperimentDialog() {
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
		alertDialogBuilder.setTitle(R.string.abort_experiment);

		alertDialogBuilder.setNegativeButton(R.string.no_resume, CancelExperimentDialog.this);
		alertDialogBuilder.setNeutralButton(R.string.exit_without_saving, CancelExperimentDialog.this);
		alertDialogBuilder.setPositiveButton(R.string.save_and_exit, CancelExperimentDialog.this);

		return alertDialogBuilder.create();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {

		switch (which) {
		case DialogInterface.BUTTON_NEGATIVE:
			dismiss();
			break;
		case DialogInterface.BUTTON_NEUTRAL:
			FileUpload.getInstance().removeFiles(ExperimentManager.getInstance().getCurrentExperiment().getStartTimestamp(),ExperimentManager.getInstance().getCurrentExperiment().getParticipantId());			
			FileUpload.getInstance().send(Config.FILE_UPLOAD_SERVER+Config.FILE_UPLOAD_PHP,"cancel", ExperimentManager.getInstance().getCurrentExperiment().getStartTimestamp(),ExperimentManager.getInstance().getCurrentExperiment().getParticipantId());	 									
			((URwalkingActivity) getActivity()).finishExperiment(false, true);	
			break;
		case DialogInterface.BUTTON_POSITIVE:
			((URwalkingActivity) getActivity()).finishExperiment(true, true);
			break;
		default:
			break;
		}

	}
}